﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using Microsoft.Xna.Framework.Content;
using System.Xml.Serialization;  // XmlSerialiser
using FlanProjectData.Helpers;

namespace FlanProjectData.GameObjects
{
    public class Inventory
    {

        private static Logger logger = new Logger("Inventory");

        #region Attributs

        public List<Item> lstItems { get; set; }

        [ContentSerializerIgnore]
        public int Size { get; set; }

        #region Ctor
        public Inventory()
        {
            this.lstItems = new List<Item>();
            Size = 0;
        }
        #endregion

        #endregion

        #region Methods

        public Item GetItemAt(int index)
        {
            Item i = null;
            if (index < this.lstItems.Count)
            {
                i = this.lstItems[index];
            }
            else
            {
                logger.Warn("No item at this position : " + index);
            }

            if (i == null)
            {
                logger.Warn("No item at this position? : " + index);
            }
            return i;
        }
        public Item getRandomItem()
        {
            Random rand = new Random();
            int randValue = rand.Next(0, this.lstItems.Count);
            return GetItemAt(randValue);
        }

        public void AdItem(Item newItem)
        {
            this.lstItems.Add(newItem);
            Size++;
        }

        public void save()
        {

            StreamWriter leStream = new StreamWriter("Content/Data/Items/lstItem.xnb");
            XmlSerializer leSerial = new XmlSerializer(typeof(Inventory));
            leSerial.Serialize(leStream, this);
            leStream.Close();

        }
        public void setItemAt(int i, Item newItem)
        {
            if (newItem != null)
            {
                this.lstItems[i] = newItem;
            }
            else
            {
                this.lstItems.RemoveAt(i);
                Size--;
            }

        }

        #endregion
    }
}
